package com.queen.restaurant.util;

import java.io.IOException;
import java.util.HashMap;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;

public class BaiduGeoUtil {

	// 百度公钥
	public static final String AK = "BB7cb5d1a52204df7f91698c544d6701";
	// LBS云端用户POI表的ID
	public static final String USER_POI_GEOTABLE_ID = "37632";
	// LBS云端用户POI表的ID
	public static final String RESTAURANT_POI_GEOTABLE_ID = "37803";
	// LBS云端创建POI数据的URL POST请求
	public static final String CREATE_POI_URL = "http://api.map.baidu.com/geodata/v2/poi/create";

	// 解析百度LBS返回的数据
	public static String createPoiResult(String baiduResult) {
		String poiId = "";
		JsonFactory factory = new MappingJsonFactory();
		try {
			JsonParser parser = factory.createJsonParser(baiduResult);
			parser.nextToken();
			// parser.nextToken();
			// String name = parser.getCurrentName();
			// System.out.println("name=" + name + " text=" +
			// parser.getIntValue());

			// 接受结果的HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			// while循环遍历Json结果
			while (parser.nextToken() != JsonToken.END_OBJECT) {
				// 跳转到Value
				parser.nextToken();
				System.out.println("name= " + parser.getCurrentName()
						+ " text=" + parser.getText());
				map.put(parser.getCurrentName(), parser.getText());
			}

			int statusCode = Integer.parseInt(map.get("status"));
			if (statusCode == 0) {
				poiId = map.get("id");
			}

		} catch (JsonParseException e) {
			poiId = "error";
		} catch (IOException e) {
			poiId = "error";
		}
		return poiId;

	}
}
